<?php
/**
 * IG_Filesystem_Dir
 *
 * @author maomao.thx@gmail.com
 * @copyright Copyright (c) 2009 Green Shore (http://www.iwgame.com)
 * @version $Id: Dir.php 869 2011-09-14 09:09:26Z mmxie $
 */
class IG_Filesystem_Dir
{
    /**
     * returns the directories in the path
     * if append path is set then this path will appended to the results
     *
     * @param string $path
     * @param string $appendPath
     * @return array
     */
    public static function getDirectories($path, $appendPath = false)
    {
        if (is_dir($path)) {
            $contents = scandir($path);
            if (is_array($contents)) {
                $returnDirs = false;
                foreach ($contents as $dir) {
                    if (is_dir($path . '/' . $dir) && $dir != '.' && $dir != '..' && $dir != '.svn') {
                        $returnDirs[] = $appendPath . $dir;
                    }
                }
                if ($returnDirs) {
                    return $returnDirs;
                }
            }
        }
    }

    /**
     * Utility function to recursive directory creation
     *
     * @param string $dir
     * @param integer $mode
     * @param boolean $recursive
     * @return boolean
     */
    public static function mkdirs($dir, $mode = 0777, $recursive = true)
    {
        if (($dir === null) || $dir === '') {
            return false;
        }
        if (is_dir($dir) || $dir === '/') {
            return true;
        }
        if (self::mkdirs(dirname($dir), $mode, $recursive)) {
            return mkdir($dir, $mode);
        }
        return false;
    }
}